package cn.chencaiju.day09;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.Axis;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart.Data;
import javafx.scene.chart.XYChart.Series;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class MyLineChart extends Application {

	@Override
	public void start(Stage primaryStage) throws Exception {
		String title="语文分数";
		Map<String,Integer> scores=new HashMap<>();
		scores.put("sd", 66);
		scores.put("qad", 77);
		scores.put("sds", 88);
		Map<String,Map<String,Integer>> map=new HashMap<>();
		map.put(title, scores);
		map.put("数学分数", scores);
		
		VBox box = new VBox();
		List<LineChart<String,Number>> elements=createLineChart(map);
		box.getChildren().addAll(elements);
		Scene scene=new Scene(box);
		
		primaryStage.setScene(scene);
		primaryStage.show();
	}
	
	/**
	 * 创建一个折线图
	 * @param title
	 * @param scores
	 * @return
	 */
	public static LineChart<String, Number> createLineChart(String title,Map<String, Integer> scores){
		Axis<String> xAxis=new CategoryAxis();
		Axis<Number> yAxis=new NumberAxis();		
		yAxis.setLabel("分数");
		xAxis.setLabel("姓名");
		LineChart<String,Number> lineChart=new LineChart<>(xAxis,yAxis);
		Series<String,Number> series=new Series<String, Number>();
		Set<Entry<String, Integer>> entrySet = scores.entrySet();
		for(Entry<String, Integer> item:entrySet) {
			series.getData().add(new Data<String,Number>(item.getKey(),item.getValue()));
		}
		series.setName(title);
		lineChart.getData().add(series);
		return lineChart;
		
	}
	
	/**
	 * 创建多个折线图
	 * @param map 嵌套集合
	 * @return
	 */
	public static List<LineChart<String,Number>> createLineChart(Map<String,Map<String,Integer>> map){
		String title=null;
		Map<String,Integer> scores=null;
		Set<Entry<String, Map<String, Integer>>> entrySet = map.entrySet();
		List<LineChart<String,Number>> result=new ArrayList<>();
		for(Entry<String, Map<String,Integer>> item:entrySet) {
			title=item.getKey();
			scores=item.getValue();
			result.add(createLineChart(title,scores));
		}
		return result;
		
	}
	public static void main(String[] args) {
		launch(args);
	}
}
